package com.china08.yxyapi.api.manage.stu.mystudy.homework.workinfo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues.Article;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;

/**
 * 答题卡
 * 
 * @author yyy
 *
 */
// @Api(tags = { "v19", "作答人自己的答案" })
@CrossOrigin
@RestController
public class HomeWorkQuestionTwoController extends YxyApiBaseController {

    @Autowired
    private HomeworkTaskReportQuesRepository htrr;
    @Autowired
    private HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    /**
     * 查询作业试题方法 2017年3月13日11:14:47
     * 
     * @param reqModel
     *            试题的ID
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/workinfo/homeworkQuestiontwo/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(@RequestBody ReqModel reqModel) {
        List<RespModel> modelList = new ArrayList<RespModel>();

        // 1.将当前任务下的所有不是通知类型的试题查出来
        List<HomeworkTaskQues> homeworkTaskQuesList = homeworkTaskQuesRepository
                .findByTaskIdAndQuesTypeNotInOrderByOrder(reqModel.taskId, "-1");
                // 2.获取到试题id

        // 将获取到的试题id封装到一个list里面
        /*
         * List<String> quesIdsList = new ArrayList<String>(); for
         * (HomeworkTaskQues homeworkTaskQues : homeworkTaskQuesList) {
         * quesIdsList.add(homeworkTaskQues.getQuesId()); }
         */
        // 3.循环封装好的试题list

        List<HomeworkTaskReportQues> lHomeworkTaskReportQues = htrr
                .findByStudentIdAndTaskIdAndQuestionTypeNotIn(reqModel.studentId, reqModel.taskId, "-1");

        RespModel model = null;
        RespModel model2 = null;
        for (HomeworkTaskQues homeworkTaskQues : homeworkTaskQuesList) {
            model = new RespModel();
            model.answerOfMy = "";
            model.order = homeworkTaskQues.getOrder();
            model.questionId = homeworkTaskQues.getQuesId();
            if (lHomeworkTaskReportQues != null && lHomeworkTaskReportQues.size() > 0) {
                for (HomeworkTaskReportQues stuQues : lHomeworkTaskReportQues) {
                    if (homeworkTaskQues.getQuesId().equals(stuQues.getQuesId())) {
                        model.answerOfMy = stuQues.getAnswerOfMy();
                        if ("2".equals(homeworkTaskQues.getQuesType())) {
                            if (homeworkTaskQues.getIfNeedSumbit()) {
                                model.answerOfMy = "已作答";
                            } else {
                                model.answerOfMy = "无需作答";
                            }
                        }
                        if ("3".equals(homeworkTaskQues.getQuesType())) {
                            model.answerOfMy = "已作答";
                        }
                        if ("4".equals(homeworkTaskQues.getQuesType())) {
                            model.answerOfMy = "已作答";
                        }
                        if ("5".equals(homeworkTaskQues.getQuesType())) {
                            List<String> list = new ArrayList<>();
                            List<Integer> looktimeLst = new ArrayList<>();
                            List<Article> quesArticle = stuQues.getQuesArticle();
                            if (quesArticle == null) {
                                quesArticle = homeworkTaskQues.getQuesArticle();
                            }
                            if (quesArticle != null) {
                                for (Article article : quesArticle) {
                                    if (article.getDuration() != null && article.getDuration() > 0) {
                                        list.add("已作答");
                                        model.answerOfMy = "已作答";
                                        looktimeLst.add(article.getDuration());
                                    } else {
                                        looktimeLst.add(0);
                                        list.add(null);
                                    }
                                }
                            }
                            model.lookTimes = looktimeLst;
                            model.answerOfMys = list;
                        }
                        model.answerOfMyString = stuQues.getAnswerText();
                        if (stuQues.getAnswerImgs() != null && stuQues.getAnswerImgs().size() > 0) {
                            model.answerImgs = stuQues.getAnswerImgs();
                        } else {
                            if ("0".equals(homeworkTaskQues.getQuesType()) || "1".equals(homeworkTaskQues.getQuesType())
                            // || "2".equals(homeworkTaskQues.getQuesType())
                                    || "5".equals(homeworkTaskQues.getQuesType())) {

                            } else {
                                if (stuQues.getAnswerText() == null || stuQues.getAnswerText().length() < 1) {
                                    if (!"无需作答".equals(model.answerOfMy)) {
                                        model.answerOfMy = "";
                                    }
                                }
                                model.answerImgs = new ArrayList<String>();
                            }
                        }
                        break;
                    }
                }
            }
            // 如果homeworktasks等于空证明学生没有回答该题，所以将答案设置为空，序号继续set到respModel里面
            // if (homeworkTaskQues.getQuesType().equals("2")) {
            // model.order = homeworkTaskQuesList.size();
            // model2 = model;
            // } else {
            modelList.add(model);
            // }
        }
        // if (model2 != null) {
        // modelList.add(model2);
        // }
        return modelList;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 任务id
        public String taskId;
        // 学生id
        public String studentId;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 题号
        public Integer order;
        // 学生答案客观
        public String answerOfMy;
        public List<String> answerOfMys;
        public List<Integer> lookTimes;
        // 学生答案主观
        public String answerOfMyString;
        // 问题ID
        public String questionId;
        // 答题图片
        public List<String> answerImgs;
    }
}
